package com.hc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hc.common.bean.Result;
import com.hc.common.bean.ResultUtil;
import com.hc.constant.SpuConst;
import com.hc.domain.Spu;
import com.hc.service.SpuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * @author hecai
 * @date 2021/12/17 9:51
 */
@Slf4j
@RestController
@RequestMapping("/spu")
public class SpuController {

    @Resource
    private SpuService spuService;

    @PostMapping
    public Result create(@RequestBody Spu spu) {
        boolean res = spuService.save(spu);
        return res ? ResultUtil.success("添加成功") : ResultUtil.fail("添加失败");
    }

    @PutMapping
    public Result updateById(@RequestBody Spu spu) {
        boolean res = spuService.updateById(spu);
        return res ? ResultUtil.success("更新成功") : ResultUtil.fail("更新失败");
    }

    @DeleteMapping("{id}")
    public Result removeById(@PathVariable Serializable id) {
        boolean res = spuService.removeById(id);
        return res ? ResultUtil.success("删除成功") : ResultUtil.fail("删除失败");
    }

    @GetMapping("{id}")
    public Result getById(@PathVariable Serializable id) {
        Spu res = spuService.getById(id);
        return ResultUtil.success("查询成功").setData(res);
    }

    @GetMapping("/getSpuByHot")
    public Result getSpuByHot() {
        Page<Spu> spuPage = new Page<>(1, 7);
        List<Spu> list = spuService.page(spuPage, new QueryWrapper<Spu>()
                .select("id", "name")
                .orderByDesc("hot")
                .eq("status", SpuConst.Status.ok)).getRecords();
        return ResultUtil.success("查询成功").setData(list);
    }


    // @PostMapping
    //public Result getByCondition(@RequestBody ProductQuery condition) {
    //    List<Product> list = productService.queryByCondition(condition);
    //    return ResultUtil.success("查询成功").setData(list);
    //}
}